package com.wxx.controller;

import com.google.common.collect.Lists;
import com.wxx.pojo.ConfigResource;
import com.wxx.pojo.User;
import com.wxx.tasks.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/test/")
public class TestController {

    @Autowired
    private ConfigResource configResource;
    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping(value = "getUrl",method = RequestMethod.GET)
    @ResponseBody
    public void getUrl(HttpServletRequest request){
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String path = this.getClass().getResource("/").getPath();
        System.out.println(realPath);
        System.out.println(path);
    }


    @RequestMapping("goBaiDu")
    public Object goBaiDu(){
        return "baidu";
    }

    @RequestMapping("goztree")
    public Object goztree(){
        return "ztree";
    }

    @RequestMapping("ztree")
    @ResponseBody
    public String ztree(){

        return "";
    }


    @RequestMapping("hello")
    public Object hello(ModelMap map){
        User user = new User();
        user.setName("sdadsa");
        user.setAge(18);
        user.setDesc("<span style='color:red'>哆啦A梦</span>");
        user.setDate(new Date());

        User user1 = new User();
        user1.setName("sea2");
        user1.setAge(19);
        user1.setDesc("<span style='color:red'>哆啦A梦2</span>");
        user1.setDate(new Date());

        User user2 = new User();
        user2.setName("sea3");
        user2.setAge(20);
        user2.setDesc("<span style='color:red'>哆啦A梦3</span>");
        user2.setDate(new Date());


        ArrayList<User> list = Lists.newArrayList();
        list.add(user);
        list.add(user1);
        list.add(user2);

        user.setList(list);
        user1.setList(list);
        user2.setList(list);

        ArrayList<User> list1 = Lists.newArrayList();
        list1.add(user);
        list1.add(user1);
        list1.add(user2);


        map.addAttribute("hello",configResource.getALISMS_ACCESS_KEYID());
        map.addAttribute("user",user);
        map.addAttribute("list",list1);
        return "index";
    }

    @RequestMapping("form")
    public Object form(User user){
        user.setDate(new Date());
        String name = user.getName();
        Integer age = user.getAge();

        System.out.println(name);
        System.out.println(age);
        return "index";
    }

}
